//
// Created by long on 2021/12/10.
//

#ifndef SHEEPRENDER_SR_COLLISION_MGR_H
#define SHEEPRENDER_SR_COLLISION_MGR_H

namespace SR {
    typedef struct sr_collision_mgr : public sr_singleton<sr_collision_mgr> {
        // 两个圆是否碰撞
        bool is_collision(sr_collision_sphere &c1, sr_collision_sphere &c2) {
            return vec_sqr_magnitude(c1.center - c2.center) <= (c1.radius + c2.radius) * (c1.radius + c2.radius);
        }

        // 圆形扫掠是否碰撞
        bool is_swept_collision(sr_collision_sphere &p0, sr_collision_sphere &q0, sr_collision_sphere &p1,
                                sr_collision_sphere &q1) {
            vec3f vp = p1.center - p0.center;
            vec3f vq = q1.center - q0.center;
            vec3f A = p0.center - q0.center;
            vec3f B = vp - vq;
            float a = vec_dot(B, B);
            float b = 2 * vec_dot(A, B);
            float c = vec_dot(A, A) - (q0.radius + p0.radius) * (q0.radius + p0.radius);
            float disc = b * b - 4 * a * c;
            if (disc >= 0) return true;
            return false;
        }
    } collision_mgr;
}

#endif //SHEEPRENDER_SR_COLLISION_MGR_H
